<?php
/* * *******************************************************************************
 * Copyright (C) 2011-2013 X2Engine Inc. All Rights Reserved.
 *
 * X2Engine Inc.
 * P.O. Box 66752
 * Scotts Valley, California 95067 USA
 *
 * Company website: http://www.x2engine.com
 * Community and support website: http://www.x2community.com
 *
 * X2Engine Inc. grants you a perpetual, non-exclusive, non-transferable license
 * to install and use this Software for your internal business purposes.
 * You shall not modify, distribute, license or sublicense the Software.
 * Title, ownership, and all intellectual property rights in the Software belong
 * exclusively to X2Engine.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT.
 * ****************************************************************************** */

/* @edition:pro */

class EditRoleAccessAction extends CAction {

    public function run(){
        if(!is_null(Yii::app()->db->getSchema()->getTable('x2_auth_cache'))){
            if(Yii::app()->hasComponent('authCache')){
                $authCache = Yii::app()->authCache;
                if(isset($authCache))
                    $authCache->clear();
            }
        }
        $model = new Roles;
        $auth = Yii::app()->authManager;
        $modules = Modules::model()->findAll();
        $authItems = array();
        $tempAuthItems = $auth->getAuthItems(1);
        foreach($tempAuthItems as $item){
            $authItems[] = $item->name;
        }
        $accessGroups = array();
        foreach($modules as $module){
            $accessGroups[$module->name][] = "-";
            $items = $this->controller->Array_Search_Preg("^".ucfirst($module->name)."(.*?)Access", $authItems);
            foreach($items as $item){
                $tempName = $authItems[$item];
                $authItems[$item] = preg_replace('/'.ucfirst($module->name).'/', '', $authItems[$item]);
                $accessGroups[$module->name][$tempName] = Formatter::deCamelCase($authItems[$item]);
            }
        }
        $dataProvider = new CActiveDataProvider('Roles');
        $roles = $dataProvider->getData();
        $arr = array();
        foreach($roles as $role){
            $arr[$role->name] = $role->name;
        }
        if(isset($_POST['Roles'])){
            $name = $_POST['Roles']['name'];
            $model = Roles::model()->findByAttributes(array('name' => $name));
            if(isset($model) || $name == 'DefaultRole'){
                if(isset($model))
                    $id = $model->id;

                $auth = Yii::app()->authManager;
                $authRole = $auth->getAuthItem($name);
                if(!isset($authRole)){
                    $authRole = $auth->createRole($model->name);
                    $auth->assign($name, $model->id);
                }
                if(isset($_POST['adminFlag'])){
                    if(!$auth->hasItemChild($name, 'administrator'))
                        $auth->addItemChild($name, 'administrator');
                    Yii::app()->controller->redirect('editRoleAccess');
                }else{
                    $auth->removeItemChild($name, 'administrator');
                }
                $modules = Modules::model()->findAll();
                $authItems = array();
                $tempAuthItems = $auth->getAuthItems(1);
                foreach($tempAuthItems as $item){
                    $authItems[] = $item->name;
                }
                $tempAuthChilren = $auth->getItemChildren($name);
                $authChildren = array();
                foreach($tempAuthChilren as $tempItem){
                    $authChildren[] = $tempItem->name;
                }
                $accessGroups = array();
                foreach($modules as $module){
                    $accessGroups[$module->name][] = "-";
                    $items = $this->controller->Array_Search_Preg(ucfirst($module->name)."(.*?)Access", $authItems);
                    foreach($items as $item){
                        $tempName = $authItems[$item];
                        $authItems[$item] = preg_replace('/'.ucfirst($module->name).'/', '', $authItems[$item]);
                        $accessGroups[$module->name][$tempName] = Formatter::deCamelCase($authItems[$item]);
                    }
                }
                foreach($modules as $module){
                    if(isset($_POST[$module->name.'Access'])){
                        $childItems = $this->controller->Array_Search_Preg("^".ucfirst($module->name)."(.*?)Access", $authChildren);
                        if(count($childItems) > 0){
                            $auth->removeItemChild($name, $authChildren[$childItems[0]]);
                        }
                        $access = $_POST[$module->name.'Access'];
                        if(!empty($access) && $access != '-')
                            $authRole->addChild($access);
                    }
                }
                if(!is_null(Yii::app()->db->getSchema()->getTable('x2_auth_cache'))){
                    if(Yii::app()->hasComponent('authCache')){
                        $authCache = Yii::app()->authCache;
                        if(isset($authCache))
                            $authCache->clear();
                    }
                }
                $this->controller->redirect('editRoleAccess');
            }
            $this->controller->redirect('editRoleAccess');
        }

        $this->controller->render('manageRoleAccess', array(
            'dataProvider' => $dataProvider,
            'model' => $model,
            'roles' => $roles,
            'accessGroups' => $accessGroups,
        ));
    }

}

?>
